package hot100;

public class LongestCommonSubsequence {
    public int longestCommonSubsequence(String text1, String text2) {
        char[] chars1 = text1.toCharArray();
        char[] chars2 = text2.toCharArray();

        int m = chars1.length;
        int n = chars2.length;

        int[][] dp = new int[m + 1][n + 1];//dp[i][j]代表到i-1，j-1为止的最长公共子序列

        for (int i = 1; i <= m; i++) {
            for (int j = 1; j <= n; j++) {
                if (chars1[i - 1] == chars2[j - 1]) {
                    dp[i][j] = dp[i - 1][j - 1] + 1;
                } else {
                    dp[i][j] = Math.max(dp[i][j],Math.max(dp[i - 1][j],dp[i][j - 1]));
                }
            }
        }

        return dp[m][n];

    }
}
